package scjp.preparation.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbQuery {

	public static void main(String[] args) {
		// Get connection, execute query, get the result set
		// and print the entries from the result rest
		try (Connection connection = DBConnection.connectToDb();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement
						.executeQuery("SELECT * FROM contact")) {
			System.out.println("ID \tfName \tlName \temail \t\tphoneNo");
			// while (resultSet.next()) {
			// System.out.println(resultSet.getInt("id") + "\t"
			// + resultSet.getString("firstName") + "\t"
			// + resultSet.getString("lastName") + "\t"
			// + resultSet.getString("email") + "\t"
			// + resultSet.getString("phoneNo"));
			// }

			int numOfColumns = resultSet.getMetaData().getColumnCount();
			while(resultSet.next()) {
				for(int i = 1; i <= numOfColumns; i++) {
					System.out.print(resultSet.getObject(i) + "\t");
				}
				System.out.println("");
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			System.exit(-1);
		}
	}
}
